package com.jml.zlhq.app.ui;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.service.HttpService;
import com.jml.zlhq.app.ui.LoginActivity.CheckBind;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.CommonDialog;
import com.loopj.android.http.TextHttpResponseHandler;

public class WebLoginActivity extends BaseActivity {
	private TextView msg;
	private EditText userid,passwd;
	private Button submit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.web_login_activity);
		
		LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
		//LinearLayout rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		//rightLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) findViewById(R.id.content_header_left_img);
		leftImage.setImageResource(R.drawable.back_selector);
		ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.GONE);
		TextView centerText = (TextView) findViewById(R.id.content_header_center_text);
		centerText.setText("使用网站账号登录");
		TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
		rightText.setText("  ");
		TextView cityChosed = (TextView) findViewById(R.id.content_header_center_text2);
		cityChosed.setVisibility(View.GONE);
				
		msg = (TextView) findViewById(R.id.login_msg);
		userid = (EditText) findViewById(R.id.login_userid);
		passwd = (EditText) findViewById(R.id.login_passwd);
		submit = (Button) findViewById(R.id.login_submit);
		submit.setOnClickListener(this);
		
//		userid.setOnFocusChangeListener(new OnFocusChangeListener() {
//			@Override
//			public void onFocusChange(View v, boolean hasFocus) {
//				Drawable drawable1 = getResources().getDrawable(hasFocus?R.drawable.loginico_user_hover:R.drawable.loginico_user);
//				drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight()); 
//				((EditText)v).setCompoundDrawables(drawable1, null, null, null);
//			}
//		});
//		passwd.setOnFocusChangeListener(new OnFocusChangeListener() {
//			@Override
//			public void onFocusChange(View v, boolean hasFocus) {
//				Drawable drawable1 = getResources().getDrawable(hasFocus?R.drawable.loginico_password_hover:R.drawable.loginico_password);
//				drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight()); 
//				((EditText)v).setCompoundDrawables(drawable1, null, null, null);
//			}
//		});
	}

	@Override
	public void loadData(Map<String, String> param) {}

	@Override
	public void clearData() {}
	

	@Override
	public void onClick(View view) {
		final String userId = userid.getText().toString();
		final String pass = passwd.getText().toString();
		switch (view.getId()) {
		case R.id.content_header_left_lay:
			finish();
			break;
		case R.id.login_forgetpassword:
			if(StringUtil.isEmpty(userId)||!StringUtil.isPhone(userId)){
				msg.setText("登录账号非手机号");
				return;
			}
			Bundle phoneBd = new Bundle();
			phoneBd.putString("phone", userId);
			UIUtil.openActivity(this, NewPassWordActivity.class, phoneBd,1);
			break;
		case R.id.content_header_right_lay:
			UIUtil.openActivity(this, RegisterActivity.class, null, 1);
			break;
		case R.id.login_submit:
			if(StringUtil.isEmpty(userId)){
				msg.setText("登录账号不能为空");
			}else if(StringUtil.isEmpty(pass)){
				msg.setText("登录密码不能为空");
			}else{
				final Map<String, String> sbmap = new HashMap<String, String>();
				sbmap.put("username", userId);
				sbmap.put("password", pass);
				if(StringUtil.isPhone(userId)){
					sbmap.put("type", "2");
				}else if(StringUtil.isEmail(userId)){
					sbmap.put("type", "3");
				}else{
					sbmap.put("type", "1");
				}
				final Handler mHandler = new Handler(){
					@Override
					public void handleMessage(Message msg) {
						switch (msg.what) {
						case 1:
							LoginActivity.afterLogined(activity,new CheckBind() {
								@Override
								public void checkBind(String phone) {
									if(StringUtil.isEmpty(phone)||!StringUtil.isPhone(phone)){
										UIUtil.openActivity(activity, BindPhoneActivity.class, null,204);
									}
								}
							});
							String isfirst = BaseTools.getFrom("isFirstLogin", appContext.getLoginUser(), "false");
							if(isfirst=="true"){
								final CommonDialog show = new CommonDialog(activity, false);
								show.show(true, true, false, "欢迎使用", "        欢迎使用赚零花钱，您已经获得50金币奖励，祝您赚零花钱愉快！", "领取", "", new OnClickListener(){
									@Override
									public void onClick(View v) {
										show.dismiss();
										finish();
									}
								}, null);
							}else{
								finish();
							}
							break;
						default:
							break;
						}
					}
				};
				final Dialog  mProDialog =  UIUtil.showLoadingDialog(activity, "登录中，请稍后...", false);
				HttpService.postByText(URLs.USER_LOGIN_SUBMIT, sbmap,
						new TextHttpResponseHandler() {
							@Override
							public void onSuccess(int arg0, Header[] arg1, String result) {
								mProDialog.dismiss();
								try {
									JSONObject json = new JSONObject(result);
									Log.i("==vcode==", result);
									if (BaseTools.getFrom("status", json).equals("0")) {
										appContext.Login(json.getJSONObject("data").toString());
										mHandler.sendEmptyMessage(1);
									} else if(BaseTools.getFrom("status", json).equals("701")){
										msg.setText("用户名或密码错误");
									}else{
										msg.setText("系统错误");
									}
								} catch (JSONException e) {
									e.printStackTrace();
								} catch (Exception e) {
									e.printStackTrace();
								}
							}

							@Override
							public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
								mProDialog.dismiss();
								msg.setText("访问错误");
							}
						});
			}
			break;
		default:
			break;
		}
	}

}
